--[[
    玩家游戏实体
]]--

local blaze = require "blaze"
local game = require "club.threecard.blackjack"

local GameObject = require "entities.interfaces.GameObject"
local Client = require "entities.interfaces.Client"
local Player = require "entities.interfaces.Player"

local M = game.kbe.entity("BlackjackPlayer", GameObject, Client, Player)

function M:__init__()
    GameObject.__init__(self)
    Client.__init__(self)
    Player.__init__(self)
end

--[[ =============================
             服务端接口
================================== ]]--

function M:bet(amount)
    self:cellCall("bet", amount)
end

function M:insure()
    self:cellCall("insure")
end

function M:split()
    self:cellCall("split")
end

function M:double_down()
    self:cellCall("double_down")
end

function M:hit()
    self:cellCall("hit")
end

function M:stand()
    self:cellCall("stand")
end

--[[ =============================
             属性变更回调
================================== ]]--

--[[ =============================
             服务端回调
================================== ]]--

function M:onEnterWorld()
    --game.store.room.on_enter(self);
end

function M:onLeaveWorld()
    game.store.room.on_leave(self);
end

function M:set_chip(old)
    game.store.room.on_set_chip({player = self, old = old, current = self.chip})
end

function M:on_bet(amount)
    self.log.debug(string.format("玩家下注：%s", amount));
    game.store.ingame.on_player_bet({player = self, amount = amount});
end

-- 最多分两份牌
-- active_hand_index 0, 1
function M:on_turn_start(countdown, active_hand_index, action_ids, splitted)
    self.log.debug(string.format("轮到座位%d的玩家了，倒计时%d毫秒 splitted:%s action_ids:%s", self.seat.no, countdown, splitted, game.dump(action_ids)))
    game.store.ingame.on_sync_actions(action_ids);
    game.store.ingame.on_player_trun({player = self, countdown = countdown, active_hand_index=active_hand_index, splitted = splitted});
end

function M:on_double()
    self.log.debug(string.format("玩家选择翻倍"));
end

function M:on_insure(amount)
    self.log.debug(string.format("座位%d的玩家选择保险", self.seat.no))
    game.store.ingame.on_player_insure({player = self, amount = amount});
end

function M:on_split(amount)
    self.log.debug(string.format("座位%d的玩家选择分牌", self.seat.no))
    game.store.ingame.on_player_split({player = self, amount = amount});
end

function M:on_deal(card_ids, hand_index)
    self.log.debug(string.format("发牌给座位%d的玩家 %s index=%d", self.seat.no, blaze.dump(card_ids), hand_index))
    game.store.ingame.on_player_deal({player = self, card_ids = card_ids, hand_index = hand_index});
end

--scene@{phase_id = 0, left_time = 0, banker_cards = {}, players = {}, action_ids = {}}
--players->item@{no = 0, hand1_cards = {}, hand2_cards = {}, active_hand_index = 0, bet_amount = 0, insured = false, doubled = false}
function M:on_game_init(scene)
    self.log.debug("on_game_init" .. blaze.dump(scene))
    game.store.ingame.on_game_init(scene);
end

return M